﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Web;
using System.Web.UI;
using ESF.General;
using ESF.General.MVP;
using System.Web.UI.WebControls;

#endregion


namespace ESF.Web.Pages
{
	public class BasePage : Page, IBaseViewer
	{
		#region Miembros de IBaseViewer

		public string frmUserHostAddress
		{
			get { return Request.UserHostAddress; }
		}


		public bool frmEnabled
		{
			set { Page.Form.Disabled = !value; }
		}


		public string frmInfoMsg
		{
			set { JS.AddWarning (this, value); }
		}


		public string frmErrorMsg
		{
			set 
			{
				JS.AddWarning (this, value); 
			}
		}
		
		
		public string frmWarnMsg
		{
			set 
			{
				JS.AddWarning (this, value); 
			}
		}

		
		public string frmUserAlias { get { return (User != null ? User.Identity.Name : null); } }


		public bool frmIsAuthenticated
		{
			get { return (User != null ? User.Identity.IsAuthenticated : false); }
		}
		
		
		public string frmApplicationCode
		{
			set { PageHelper.SetApplicationCode (Page, value); }
			get { return PageHelper.GetApplicationCode (Page); }
		}

		public string frmApplicationName
		{
			get { return PageHelper.GetApplicationName(Page); }
			set { PageHelper.SetApplicationName(Page, value); }
		}


		public string frmTitle
		{	
			set 
			{
				Label lblTitle;
			
			
				lblTitle = FindControl<Label> ("lblTitle");
			
				if (lblTitle != null)
				{
					lblTitle.Text = Title;
				}
			}
		}


		public int CurrentAdvance100 { set; get; }

		public string CurrentStepName { set; get; }

		#endregion
		
		
		#region Funciones
		
		protected override void OnInit(EventArgs e)
        {
			string title;


			if (!IsPostBack)
			{
				title = Request.QueryString ["frmTitle"];

				if (!string.IsNullOrEmpty (title))
				{
					Title = TextHelper.DecryptRSA (title);
				}
				
				frmTitle = Title;
			}
		}


		public void ShowClearProgressDialog ()
		{
			
		}


		public void LogError (Exception pEx)
		{
			Log.Error (pEx);
			
			frmErrorMsg = GenericException.GetClientMessage (pEx);
		}


		public T FindControl<T>(string id) where T : Control
	   {
	       return FindControl<T>(Page, id);
	   }
	   
	   public static T FindControl<T>(Control startingControl, string id) where T : Control
	   {
			T found = default(T);
	  
			int controlCount = startingControl.Controls.Count;
	  

			if (controlCount > 0)
			{
				for (int i = 0; i < controlCount; i++)
				{
					Control activeControl = startingControl.Controls[i];
					if (activeControl is T)
					{
						found = startingControl.Controls[i] as T;
						if (string.Compare(id, found.ID, true) == 0) break;
						else found = null;
					}
					else
					{
						found = FindControl<T>(activeControl, id);
						if (found != null) break;
					}
				}
			}

			return found;
		}


		public virtual void CloseApplication () { }


		protected virtual void OnClear () {}

		#endregion
		
		
		#region Funciones ViewState

		protected override PageStatePersister PageStatePersister
		{
			get
			{
				return new SessionPageStatePersister(this);
			}
		}
				
		#endregion

		public int? ItemID
		{
			get
			{
				throw new NotImplementedException();
			}
			set
			{
				throw new NotImplementedException();
			}
		}
	}
}
